﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Data;
using DaInterFace;

namespace DaFunction
{
    public class Config : IConfig
    {
        private Dictionary<string, string> _config = new Dictionary<string, string>();

        public Config()
        {
            LoadConfig();
        }

        public String Get(String key)
        {
            if (_config.Count == 0) LoadConfig();
            if (_config.ContainsKey(key))
            {
                return _config[key];
            }
            else
            {
                return "";
            }
        }

        private void LoadConfig()
        {
            DataSet data = null;
            _config.Clear();
            data = Access.GetData("SELECT FKey,FValue FROM Config");
            foreach (DataRow row in data.Tables[0].Rows)
            {
                _config.Add(row["FKey"].ToString(), row["FValue"].ToString());
            }
        }

        public void Set(String key, String value)
        {
            if (_config.ContainsKey(key))
            {
                if (_config[key] != value)
                {
                    Access.Update(String.Format("Update Config Set FValue='{0}' Where FKey='{1}'", value, key));
                    _config[key] = value;
                }
            }
            else
            {
                Access.Update(String.Format("Insert Into Config (FValue,FKey) Values ('{0}','{1}')", value, key));
                _config.Add(key, value);
            }
        }
    }
}
